package org.springbus.ff.util;

import org.springbus.ff.animate.AniFilter;
import org.springbus.ff.filter.Filter;

import java.nio.channels.Pipe;
import java.util.*;

public class Utils {
    public static double angleToRadian(double angle, int s) {
        return floor((angle / 180) * Math.PI, s);
    }

    public static double angleToPI(double angle, int s) {
        double pi = angleToRadian(angle, s);
        return pi * Math.PI;
    }

    public static double floor(double n) {
        double k = Math.pow(10, 2);
        return Math.floor(n * k) / k;
    }

    public static double floor(double n, int s) {
        double k = Math.pow(10, s);
        return Math.floor(n * k) / k;
    }

    public static String replacePlusMinus(String str) {
        return str
                .replace("+-", "-")
                .replace("-+", "-")
                .replace("--", "+")
                .replace("++", "+")
                .replace("(t-0)", "t")
                .replace("(on-0)", "on");
    }


    public static List<AniFilter> sortArrayByKey(List<AniFilter> cfilters, String key, String val) {
        List<AniFilter> carr = new ArrayList<>();
        for (AniFilter f : cfilters) {
            if (f.getShowType().equals(val)) {
                carr.remove(f);
            } else {
                carr.add(f);
            }
        }
        return carr;
    }


    /**
     * Generate auto-increment id based on type
     * @param {string} type - type
     * @return {string} id xxxx_10
     * @public
     */
    private static Map<String,Integer> cache=new HashMap<>();

   public static String genId(String type) {
       cache.putIfAbsent(type, 0);
       int id = cache.get(type);
       int next = id + 1;
       cache.put(type, id + 1);
       return type + String.valueOf(next);
   }

   public  static  String  uid() {
       return UUID.randomUUID().toString().replace("-", "");
   }

   public static void  deleteArrayElement(List<AniFilter> filters,AniFilter aniFilter){
       filters.remove(aniFilter);
   }
}
